﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapQVW_EKPOS(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQVW_EKPOS(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            Result = QueryQVW_EKPOS(dwData);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapQVW_EKPOS(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertQVW_EKPOS(dwData, 1);
            }
            UpdateQVW_EKPOS(dwLocal);

        }
        public void InsertQVW_EKPOS(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1://20140501
                    or1.CommandText = "INSERT INTO QVW_EKPOS  "
                        + " SELECT DISTINCT  EKPNEUD AS NDATE,SUBSTR(EKPNEUD,1,4) AS NYEAR,SUBSTR(EKPNEUD,5,2) AS NMONTH,SUBSTR(EKPNEUD,7,2) AS NDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(EKPNEUD,5,2))/3) AS NQUARTER,  'B'||EKPAUFKRS||EKPAUFLFD AS KAUF,'B' AS AUFTYP,'B'||EKPAUFKRS||EKPAUFLFD||EKPAUFPOS AS PAUF ,EKPAUFKRS AS PKRS ,EKPAUFLFD AS PLFD,EKPAUFPOS AS PPOS,EKPLIEFNR AS LSNR,EKPNEUB AS KSTAFF, EKPPOSTYP,EKPWAKZ AS ARTST,utl_raw.cast_to_varchar2(EKPART) AS PART,utl_raw.cast_to_varchar2(EKPVAR) AS PVAR,utl_raw.cast_to_varchar2(EKPFARBE) AS PCOLOR,EKPDESS AS PDESS,EKPCOL AS PDCWY,EKPBESCH AS PFINISH,EKPGROE AS PSIZE,EKPMEEH,EKPPRME,EKPPREH,EKPPREHFKT,EKPME1,EKPWE1,EKPME3,EKPWE3,ROUND(EKPPRME*0.001,2)  AS EKPPREIS,  ROUND(EKPME1*0.01*EKPMEEHFKT*0.000001*EKPPRME*0.001,2)  AS EKPVALUE ,ROUND(EKPME1*0.01*EKPMEEHFKT*0.000001,2) AS EKPQTY,ROUND(EKPME2*0.01*EKPMEEHFKT*0.000001,2) AS QRECEIVE,EKPLTERM AS DDAT,SUBSTR(EKPLTERM,1,4) AS DYEAR,SUBSTR(EKPLTERM,5,2) AS DMONTH,SUBSTR(EKPLTERM,7,2) AS DDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(EKPLTERM,5,2))/3) AS DQUARTER ,EKPDRUDAT AS DRDAT,SUBSTR(EKPDRUDAT,1,4) AS DRYEAR,SUBSTR(EKPDRUDAT,5,2) AS DRMONTH,SUBSTR(EKPDRUDAT,7,2) AS DRDAY,'Q'||CEIL(TO_NUMBER(SUBSTR(EKPDRUDAT,5,2))/3) AS DRQUARTER,EKPWVTERM AS VTERMDAT,SUBSTR(EKPWVTERM,1,4) AS VYEAR,SUBSTR(EKPWVTERM,5,2) AS VMONTH,SUBSTR(EKPWVTERM,7,2) AS VDAY,EKPWUTERM AS UTERMDAT,SUBSTR(EKPWUTERM,1,4) AS UYEAR,SUBSTR(EKPWUTERM,5,2) AS UMONTH,SUBSTR(EKPWUTERM,7,2) AS UDAY,EKPAEND AS RDATE,SUBSTR(EKPAEND,1,4) AS RYEAR,SUBSTR(EKPAEND,5,2) AS RMONTH,SUBSTR(EKPAEND,7,2) AS RDAY,CASE  WHEN  EKPAEND>EKPWUTERM THEN 'N' ELSE 'Y' END AS EKOTD "
                        +" FROM EKPOS WHERE EKPSA='PO' AND EKPMANDANT=:MANDANT";
                    or1.Parameters.Add("MANDANT", OracleType.NVarChar).Value = dwData["MANDANT"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public void DeleteQVW_EKPOS(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM QVW_EKPOS X  ";
                    break;  
                default:
                    break;
            }
            Connect2Proxy(or1);
        }
        public DataSet QueryQVW_EKPOS(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "SELECT * FROM QVW_EKPOS "; ;
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "QVW_EKPOS";
            Return = Result;
            return Return;
        }
        public void UpdateQVW_EKPOS(DataRow dwData, int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1: 
                    break;
            }
            Connect2Proxy(or1);
        }
    }
}
